/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import com.opensymphony.xwork2.ActionSupport;
import debug.Debugger;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import pojo.LotteryDrawHistory;
import pojo.User;
import service.ILotteryService;
import service.IUserService;

/**
 *
 * @author beryl
 */
public class LotteryAction extends ActionSupport {

    private IUserService<User> userService;
    private ILotteryService<LotteryDrawHistory> lotteryService;

    public IUserService<User> getUserService() {
        return userService;
    }

    public void setUserService(IUserService<User> userService) {
        this.userService = userService;
    }

    public ILotteryService<LotteryDrawHistory> getLotteryService() {
        return lotteryService;
    }

    public void setLotteryService(ILotteryService<LotteryDrawHistory> lotteryService) {
        this.lotteryService = lotteryService;
    }
    private LotteryDrawHistory current_draw;
    private List<LotteryDrawHistory> drawlist;

    public LotteryDrawHistory getCurrent_draw() {
        return current_draw;
    }

    public void setCurrent_draw(LotteryDrawHistory current_draw) {
        this.current_draw = current_draw;
    }

    public List<LotteryDrawHistory> getDrawlist() {
        return drawlist;
    }

    public void setDrawlist(List<LotteryDrawHistory> drawlist) {
        this.drawlist = drawlist;
    }
    private int lotteryListSize = 10;

    public int getLotteryListSize() {
        return lotteryListSize;
    }

    public void setLotteryListSize(int lotteryListSize) {
        this.lotteryListSize = lotteryListSize;
    }
    private int userCurrentTicketCount;
    private long timerCountDown; // in sec

    public long getTimerCountDown() {
        return timerCountDown;
    }

    public void setTimerCountDown(long timerCountDown) {
        this.timerCountDown = timerCountDown;
    }

    public int getUserCurrentTicketCount() {
        return userCurrentTicketCount;
    }

    public void setUserCurrentTicketCount(int userCurrentTicketCount) {
        this.userCurrentTicketCount = userCurrentTicketCount;
    }

    private void fetchDrawlist() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession(false);
        drawlist = this.lotteryService.listPastDraws(lotteryListSize);
        current_draw = null;
        if (drawlist == null) {
            return;
        }
        Debugger.msg("Draws fetched: " + drawlist.size());
        current_draw = drawlist.get(0);
        timerCountDown = (current_draw.getDrawdate().getTime() - (new Date().getTime())) / 1000;
        Debugger.msg("time left: " + timerCountDown);
        userCurrentTicketCount = this.lotteryService.countTicket(
                (String) session.getAttribute("uni"),
                current_draw.getDrawid());
        Debugger.msg("Count tic: " + userCurrentTicketCount);
        session.setAttribute("user", this.userService.findUserByUni((String) session.getAttribute("uni")));
    }

    public String listDraws() {
        Debugger.msg("listDraws() called in LotteryAction");
        fetchDrawlist();
        return "lottery";
    }

    public String buyTicket() {
        Debugger.msg("buyTicket() called in LotteryAction");
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession(false);
        this.lotteryService.buyTicket((User) session.getAttribute("user"));
        return listDraws();
    }
}
